﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DanhMuc_QuanLyDanhMuc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindLoaiDM();
        }
    }
    private void BindLoaiDM()
    {
        _dbDataContext _db = new _dbDataContext();
        ListItem item = new ListItem("---Chọn loại danh mục---","-1");
        drlLoai.Items.Add(item);
        var val = _db.tblLoaiDanhMucs.Where(r=>r.LoaiDM!=9&&r.LoaiDM!=1014 && r.LoaiDM!=7).ToList();
        foreach (var items in val)
        {
            item = new ListItem(items.TenLoai.Trim(),items.LoaiDM.ToString());
            drlLoai.Items.Add(item);
        }
    }
   
    private void BindGrid(int loaiid)
    {
        _dbDataContext _db = new _dbDataContext();
        if (loaiid == -1)
        {
            var val = from p in _db.tblDanhMucs select p;// select new { ID=p.ID,TenPhuong=p.TenPhuong.Trim(),QuanID=p.QuanID};
            grid.DataSource = val.ToList();
            grid.DataBind();
        }
        else
        {
            var val = from p in _db.tblDanhMucs where p.Loai == int.Parse(drlLoai.SelectedValue) select p;// select new { ID=p.ID,TenPhuong=p.TenPhuong.Trim(),QuanID=p.QuanID};
            grid.DataSource = val.ToList();
            grid.DataBind();
        }
    }
    protected void drlLoai_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid(int.Parse(drlLoai.SelectedValue.ToString()));
        lblmessage.Text = "";
    }
    protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(grid.DataKeys[e.RowIndex].Value.ToString());
        if (CatalogAccess.DeleteDanhMuc(id))
        {
            BindGrid(int.Parse(drlLoai.SelectedValue));
        }
    }
    protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = int.Parse(grid.DataKeys[e.RowIndex].Value.ToString());
        string tendanhmuc = ((TextBox)grid.Rows[e.RowIndex].FindControl("txtTenDanhMuc")).Text;
        if (CatalogAccess.UpdateDanhMuc(id, tendanhmuc))
        {
            grid.EditIndex = -1;
            BindGrid(int.Parse(drlLoai.SelectedValue));
        }
    }
    protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grid.EditIndex = e.NewEditIndex;
        BindGrid(int.Parse(drlLoai.SelectedValue.ToString()));
    }
    protected void grid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grid.EditIndex = -1;
        BindGrid(int.Parse(drlLoai.SelectedValue.ToString()));
    }
    protected void btnadd_Click1(object sender, EventArgs e)
    {
       
        if (int.Parse(drlLoai.SelectedValue) == -1)
        {
            lblmessage.Text = "Chưa chọn loại danh mục";
        }
        else
        {
            if (txtTenDanhMuc.Text.Length == 0)
            {
                lblmessage.Text = "Chưa nhập tên danh mục";
            }
            else
            {
                if (CatalogAccess.InsertDanhMuc(txtTenDanhMuc.Text,int.Parse(drlLoai.SelectedValue.ToString())))
                {
                    lblmessage.Text = "Đã thêm thành công!";
                    BindGrid(int.Parse(drlLoai.SelectedValue));
                    txtTenDanhMuc.Text = "";
                }
                else
                {
                    lblmessage.Text = "Không thêm được, xin kiểm tra lại!";
                }
            }
        }
    
    }
}